package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.service.IDepartmentSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentSericeImpl extends BaseServiceImpl<Department> implements IDepartmentSerice {
    @Autowired
    private DepartmentMapper Mapper;

    @Override
    public List<Employee> getManager() {
        return Mapper.getManager();
    }

    private void loadMultistage(List<Department> departments) {
        if(departments.size()!=0) {
            for (Department dept : departments) {
                //根据父id获取children
                List<Department> children = Mapper.getChildren(dept.getId());
                //将children放在dept里
                dept.setChildren(children.size() == 0 ? null : children);
                //把下级所有部门再次进行查询下下级
                this.loadMultistage(children);
            }
        }
    }


    @Override
    public List<Department> getOneAndTwo() {

        List<Department> list = Mapper.getOneAndTwo();
        for (Department dept:list) {
            List<Department> children = dept.getChildren();
            if (children.size()==0){
                dept.setChildren(null);
            }
            this.loadMultistage(children);
        }
        return list;
    }
}
